// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   QuickAuthenticator.java

package org.quickserver.net.server;

import java.io.*;
import org.quickserver.net.AppException;
import org.quickserver.net.ConnectionLostException;
import org.quickserver.util.io.ByteBufferInputStream;

// Referenced classes of package org.quickserver.net.server:
//            Authenticator, DataType, ClientHandler, DataMode, 
//            ClientEvent

public abstract class QuickAuthenticator
    implements Authenticator
{

    public QuickAuthenticator()
    {
    }

    public abstract boolean askAuthorisation(ClientHandler clienthandler)
        throws IOException, AppException;

    public void sendString(ClientHandler clientHandler, String msg)
        throws IOException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.STRING)
                clientHandler.setDataMode(DataMode.STRING, DataType.OUT);
            clientHandler.sendClientMsg(msg);
        }
    }

    public String askStringInput(ClientHandler clientHandler, String msg)
        throws IOException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.STRING)
                clientHandler.setDataMode(DataMode.STRING, DataType.OUT);
            clientHandler.sendClientMsg(msg);
        }
        if(clientHandler.getDataMode(DataType.IN) != DataMode.STRING)
            clientHandler.setDataMode(DataMode.STRING, DataType.IN);
        String data = null;
        if(clientHandler.hasEvent(ClientEvent.RUN_BLOCKING))
        {
            data = clientHandler.getBufferedReader().readLine();
        } else
        {
            ByteBufferInputStream bbin = (ByteBufferInputStream)clientHandler.getInputStream();
            data = bbin.readLine();
        }
        if(data != null)
            return data;
        else
            throw new ConnectionLostException();
    }

    public void sendObject(ClientHandler clientHandler, Object msg)
        throws IOException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.OBJECT)
                clientHandler.setDataMode(DataMode.OBJECT, DataType.OUT);
            clientHandler.sendClientObject(msg);
        }
    }

    public Object askObjectInput(ClientHandler clientHandler, Object msg)
        throws IOException, ClassNotFoundException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.OBJECT)
                clientHandler.setDataMode(DataMode.OBJECT, DataType.OUT);
            clientHandler.sendClientObject(msg);
        }
        if(clientHandler.getDataMode(DataType.IN) != DataMode.OBJECT)
            clientHandler.setDataMode(DataMode.OBJECT, DataType.IN);
        Object data = clientHandler.getObjectInputStream().readObject();
        if(data != null)
            return data;
        else
            throw new ConnectionLostException();
    }

    public void sendByte(ClientHandler clientHandler, String msg)
        throws IOException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.BYTE)
                clientHandler.setDataMode(DataMode.BYTE, DataType.OUT);
            clientHandler.sendClientBytes(msg);
        }
    }

    public String askByteInput(ClientHandler clientHandler, String msg)
        throws IOException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.BYTE)
                clientHandler.setDataMode(DataMode.BYTE, DataType.OUT);
            clientHandler.sendClientBytes(msg);
        }
        if(clientHandler.getDataMode(DataType.IN) != DataMode.BYTE)
            clientHandler.setDataMode(DataMode.BYTE, DataType.IN);
        String data = clientHandler.readBytes();
        if(data != null)
            return data;
        else
            throw new ConnectionLostException();
    }

    public void sendBinary(ClientHandler clientHandler, byte msg[])
        throws IOException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.BINARY)
                clientHandler.setDataMode(DataMode.BINARY, DataType.OUT);
            clientHandler.sendClientBinary(msg);
        }
    }

    public byte[] askBinaryInput(ClientHandler clientHandler, byte msg[])
        throws IOException
    {
        if(msg != null)
        {
            if(clientHandler.getDataMode(DataType.OUT) != DataMode.BINARY)
                clientHandler.setDataMode(DataMode.BINARY, DataType.OUT);
            clientHandler.sendClientBinary(msg);
        }
        if(clientHandler.getDataMode(DataType.IN) != DataMode.BINARY)
            clientHandler.setDataMode(DataMode.BINARY, DataType.IN);
        byte data[] = clientHandler.readBinary();
        if(data != null)
            return data;
        else
            throw new ConnectionLostException();
    }
}
